﻿#include "AntiBotModule.h"

#define HELIUM_ANTIBOT_ADVANCE

std::shared_mutex botlist_lock{};
std::map<uint64_t, Antibot_struct> botlist{};

SettingEnum AntiBotEnum_AntiBotModule{{{{u8"normal", u8"一般", u8"一般", u8"普通"}, {u8"Provide some basic bypass functions", u8"提供一些基础绕过功能", u8"提供一些基礎繞過功能", u8"いくつかの基本的なバイパス機能を提供します"}},
									   {{u8"advance", u8"高级", u8"高級", u8"事前"}, {u8"Calculate using AI model", u8"使用AI模型计算", u8"使用AI模型計算", u8"AIモデルで計算"}}}};

void init_AntibotModule(IModule* i) {
}

void onEnable_AntibotModule() {
}

void onDisable_AntibotModule() {
	[[maybe_unused]] auto lock = std::shared_lock(botlist_lock);
	botlist.clear();
}

void onWorldTick_AntibotModule(C_GameMode*) {

}

void onTick_AntibotModule(C_GameMode* gm) {
	constexpr size_t i = sizeof(Antibot_struct);

	const C_LocalPlayer* localPlayer = GameData::getLocalPlayer();
	const auto localPlayer_pos = localPlayer->getPos();

	GameData::forEachEntity([localPlayer, localPlayer_pos](C_Entity* ent) {
		if (ent->isPlayer()) {
			[[maybe_unused]] auto lock = std::shared_lock(botlist_lock);
			const auto uniqueid = ent->getUniqueId();

			if (botlist.contains(uniqueid)) {
				// to do ...

			} else {
				const auto angle = normAngles(CalcAngle(*localPlayer_pos, *ent->getPos()));
				botlist.insert({uniqueid, Antibot_struct{ent,
														 true,
														 antibot_vector::vector<uint32_t>{1u},
														 antibot_vector::vector<uint32_t>{},
														 antibot_vector::vector<float>{ent->getTicksPerSecond()},
														 *ent->getPos(),
														 0u,
														 ent->onGround,
														 0u,
														 {vec3_t{angle.x, angle.y, 0.f}},
														 antibot_vector::vector<bool>{localPlayer->canSee(ent)},
														 1u,
														 static_cast<uint32_t>(ent->isInWall()),
														 static_cast<uint32_t>(ent->isInvisible()),
														 static_cast<uint32_t>(ent->isInLava()),
														 static_cast<uint32_t>(ent->isInWater()),
														 static_cast<uint32_t>(ent->isOnFire()),
														 ent->canShowNameTag()}});
			}
		}
	});
}

void onAttack_AntibotModule(C_Entity*) {

}

void onPreRender_AntibotModule(C_MinecraftUIRenderContext* c) {

}

void onPostRender_AntibotModule(C_MinecraftUIRenderContext* c) {

}

void onLevelRender_AntibotModule() {

}

void onMove_AntibotModule(C_MoveInputHandler*) {

}

void onSendPacket_AntibotModule(C_Packet*, bool&) {

}

void onPlayerTick_AntibotModule(C_Player*) {

}

void onGetPickRange_AntibotModule() {

}

void onUpdate_AntibotModule() {

}

IModule AntiBotModule{
	Category::COMBAT,
	{u8"AntiBot", u8"反假人", u8"反假人", u8"アンチボット"},
	{u8"Test the server dummy and bypass", u8"检测服务器假人并绕过", u8"檢測服務器假人並繞過", u8"サーバーのダミーを検出してバイパスする"},
	0,  // keybind
	{SettingEntry{
		{u8"AntiBot option", u8"反假人选项", u8"反假人選項", u8"アンチダミーオプション"},
		&AntiBotEnum_AntiBotModule,
		0,
		{u8"Set anti-bot mode", u8"设置反假人模式", u8"設置反假人模式", u8"アンチダミーモードを設定する"}}},
	init_AntibotModule,
	onEnable_AntibotModule,
	onDisable_AntibotModule,
	onWorldTick_AntibotModule,
	onTick_AntibotModule,
	nullptr,
	onAttack_AntibotModule,
	onPreRender_AntibotModule,
	onPostRender_AntibotModule,
	onLevelRender_AntibotModule,
	onMove_AntibotModule,
	onSendPacket_AntibotModule,
	onPlayerTick_AntibotModule,
	onGetPickRange_AntibotModule,
	onUpdate_AntibotModule};

bool isBot(C_Entity* ent) {
	auto localPlayer = GameData::getLocalPlayer();

	if (ent == localPlayer) [[unlikely]]
		return false;

	const auto antibot_mode = *AntiBotModule.settings.front().value._int;
#ifdef HELIUM_ANTIBOT_ADVANCE
	if (antibot_mode == 0) {
		if constexpr (true) {  // hithox
			/*
			正常玩家的碰撞箱：1.8f*0.6f
			CubeCraft玩家下蹲时的碰撞箱：1.65f*0.6f
			使用鞘翅：0.6f*0.6f
			疾跑游泳：0.6f*0.6f
			使用激流三叉戟：0.6f*0.6f
			*/
			const float height = ent->height;
			if (height > 1.8f)
				return false;

			if (height < 1.79f && height > 1.65f)
				return false;

			if (height < 1.649f && height > 1.6f)
				return false;

			if (const float width = ent->width; width > 0.6f || width < 0.59f)
				return false;
		}

		if constexpr (false) {  // name check
			const auto targetName = ent->getNameTag()->getString();

			if (ent->getNameTag()->getTextLength() == 0)
				return false;

			if (localPlayer->canShowNameTag() && !ent->canShowNameTag())
				return false;

			if ((localPlayer->getNameTag()->getString()).find(u8"\n") == std::u8string_view::npos && targetName.find(u8"\n") != std::u8string_view::npos)
				return false;
		}

		return true;
	} else {



		return false;
	}
#else
	if constexpr (true) {  // hithox
		/*
		正常玩家的碰撞箱：1.8f*0.6f
		CubeCraft玩家下蹲时的碰撞箱：1.65f*0.6f
		使用鞘翅：0.6f*0.6f
		疾跑游泳：0.6f*0.6f
		使用激流三叉戟：0.6f*0.6f
		*/
		const float height = ent->height;
		if (height > 1.8f)
			return false;

		if (height < 1.79f && height > 1.65f)
			return false;

		if (height < 1.649f && height > 1.6f)
			return false;

		if (const float width = ent->width; width > 0.6f || width < 0.59f)
			return false;
	}
#endif  // HELIUM_ANTIBOT_ADVANCE
	std::unreachable();
}
